package com.yzc.controller;


import com.yzc.model.entity.system.RegisterUser;
import com.yzc.result.Result;
import com.yzc.result.ResultCodeEnum;
import com.yzc.service.RegisterService;

import com.yzc.util.BCrypt;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/userapi")
public class RegisterController {

    @Autowired
    private RegisterService registerService;
    @Autowired
    private BCrypt bCrypt;

    private static final Marker LOG_MARKER = MarkerFactory.getMarker("LOG");

    @PostMapping("/register")
    public Result<Void> register(@RequestBody RegisterUser user) {
        String encode = bCrypt.encode(user.getPass());
        user.setPass(encode);
        int addUserResult = registerService.addUser(user);
        if (addUserResult == 1) {
            log.info(LOG_MARKER, "注册成功,返回值：{}", addUserResult);
            return Result.success();
        } else {
            log.error(LOG_MARKER, "注册失败,返回值：{}", addUserResult);
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }
}


